<template>
  <Container>
    <Tabs v-model="tab">
      <TabPane label="经销商积分有效期设置" name="tab1">
        <Form :model="form" :label-width="170" ref="form" :rules="rules">
          <FormItem label="经销商积分有效期设置:" prop="fieldValue">
            <InputNumber :max="31" :min="1" v-model="form.fieldValue" style="width:200px"></InputNumber>天
          </FormItem>
        </Form>
      </TabPane>
      <Loader :status="status" @refresh="getData"></Loader>
    </Tabs>
    <div slot="footer" class="container-footer bg-white">
      <Button :disabled="disabled" @click="onSave" type="primary" size="large">确定</Button>
    </div>
  </Container>
</template>
<script>
export default {
  data() {
    return {
      tab: "tab1",
      status: "pass",
      disabled: false,
      form: {
        fieldValue: "",
        id: "",
        fieldKey: "",
        supplierId: ""
      },
      rules: {
        fieldValue: [
          {
            required: true,
            message: "时间不能为空",
            type: "number",
            trigger: "change"
          }
        ]
      }
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      this.status = "loading";
      let params = {
        type: 1
      };
      this.$ajaxGet("/distributor/IntegralDictionary/get", params)
        .then(res => {
          let { fieldKey, id, fieldValue, supplierId } = res;
          this.form = res;
          this.status = "pass";
        })
        .catch(() => {
          this.status = "error";
        });
    },
    onSave() {
      if (this.disabled) return;
      this.$refs["form"].validate(valid => {
        if (valid) {
          let text = this.form.id ? "编辑" : "新增";
          let url = "/distributor/IntegralDictionary/modify";
          let distributorIntegralDictionaryDTO = {
            fieldKey: this.form.fieldKey,
            fieldValue: this.form.fieldValue,
            id: this.form.id,
            supplierId: this.form.supplierId
          };
          this.status = "loading";
          this.$jsonPost(url, distributorIntegralDictionaryDTO)
            .then(data => {
              this.$Notice.success({
                title: `${text}成功`
              });
              this.status = "pass";
              this.disabled = false;
            })
            .catch(() => {
              this.status = "pass";
              this.disabled = false;
            });
        } else {
          this.disabled = false;
        }
      });
    }
  }
};
</script>